﻿using Domain.Entities;
using Domain.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Utility;


namespace EFcore.Repositories
{
    public class MenuRepository : BaseRepository<Menu>, IMenuRepository
    {
        public MenuRepository(JFDbContext dbcontext) : base(dbcontext)
        {

        }


        /// <summary>
        /// 根据Id获取实体
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public Menu GetById(int id)
        {
            var model = _dbContext.Set<Menu>().FirstOrDefault(i => i.Id == id);
            return model;
        }


        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <param name="id">Id</param>
        /// <returns></returns>
        public IQueryable<Menu> GetAll(string parentid)
        {
            if (string.IsNullOrEmpty(parentid))
            {
                return _dbContext.Set<Menu>();
            }
            else
            {
                return _dbContext.Set<Menu>().Where(i => i.Parent_Id == Int32.Parse(parentid));
            }
            
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="SearchKeyWords">查询关键字</param>
        /// <param name="PageSize">页面输出信息行数</param>
        /// <param name="PageCurrent">当前页数</param>
        /// <param name="OrderField">排序字段</param>
        /// <param name="OrderDirection">排序</param>
        /// <returns></returns>
        public IQueryable<Menu> GetPageList(string SearchKeyWords, int PageSize, int PageCurrent, string OrderField, bool OrderDirection = true)
        {
            //判断查询参数
            Expression<Func<Menu, bool>> where = LinqUtil.True<Menu>();
            if (!string.IsNullOrEmpty(SearchKeyWords))
            {
                where = where.AndAlso(i => i.Menu_name.Contains(SearchKeyWords));
            }

            var result = from p in _dbContext.Set<Menu>()
                         select p;
            if (where != null)
                result = result.Where(where);

            if (OrderField != null)
            {
                result = result.OrderBy(OrderField, OrderDirection);
            }
            else
            {
                if (OrderDirection)
                    result = result.OrderBy(m => m.Id);
                else
                    result = result.OrderByDescending(m => m.Id);
            }

            return result.Skip((PageCurrent - 1) * PageSize).Take(PageSize);

        }

        /// <summary>
        /// 根据ID是否存在，新增或修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddOrUpdate(Menu model)
        {
            if (InsertOrUpdate(model, true) != null)
            { return true; }
            else
            { return false; }
        }

        public bool DeleteById(int id)
        {
            try
            {
                Delete(id, true);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
